
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import java.util.ArrayList;


import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;

public class Character 
{

    private int difx;    
    private int x;
    private int y;
    private Image image;
    private ArrayList<Bullet> bullets;
    private int score;
   
    //Creates a new character object and creates a new set of bullets that will be associated with that character
    public Character()
    {
        ImageIcon ii = new ImageIcon("CharTemp.png", "character");
        image = ii.getImage();
        bullets = new ArrayList<Bullet>();
        x = 100;
        y = 100;
        
        
        
    }




    //Moves the character whenever the correct buttons are pressed which will change
    //the difference value. Unless the character has reached the boundaries of the game
    public void movex() 
    {
    	if(((x + difx) < 385) && ((x + difx) > 0))
    	{
    		x += difx;
    	}
        
    }
    
    public void movey(int dify)
    {
    	if(((y + dify) > 0) && ((y + dify) < 600))
    	{
    		y += dify;
    	}
    }
    
    public void increaseScore(int difScore)
    {
    	score= score + difScore;
    }
    
    public void decreaseScore(int difScore)
    {
    	score= score - difScore;
    }

  
    //Interprets the button that has been pressed and calls the aprropriate method
    public void keyPress(KeyEvent e) 
    {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) 
        {
            difx = -1;
        }

        if (key == KeyEvent.VK_RIGHT) 
        {
            difx = 1;
        }
        
        if (key == KeyEvent.VK_SPACE) {
            fire();
        }
      
    }

    //After the button has been released it sets everything back to normal
    //and stop the character moving
    public void keyRelease(KeyEvent e) 
    {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) 
        {
            difx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) 
        {
            difx = 0;
        }

    }
    
    //Adds a new bullet, to the array list, at the position of the character
    public void fire() {
        bullets.add(new Bullet(x + 70, y + 50));
        
        try{
            File fireSound =new File("FIRE!.wav");
    		AudioInputStream audioIn = AudioSystem.getAudioInputStream(fireSound);
    		Clip fire = AudioSystem.getClip();
    		fire.open(audioIn);
    		fire.start();
    		 }catch (UnsupportedAudioFileException e) 
    		 {
    	         e.printStackTrace();
    	      }
    		 catch (IOException e) 
    		 {
    	         e.printStackTrace();
    	      }
    		 catch (LineUnavailableException e) 
    	      {
    	         e.printStackTrace();
    	      }
    }
    
    //Basic getter methods
    public ArrayList<Bullet> getBullets() {
        return bullets;
    }
    
    public int getX() 
    {
        return x;
    }

    public int getY() 
    {
        return y;
    }

    public Image getImage() 
    {
        return image;
    }
    
    public int getScore()
    {
    	return score;
    }
    
}